// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var awayEntry ;

$(function () {


$(".rule-select-radio").change(function() {
	if (this.name == "project")
	   $('input[name=action_type]:eq(0)').attr('checked', 'checked');
else if (this.name == "personal")
$('input[name=action_type]:eq(1)').attr('checked', 'checked');


	
	});


	floatingToolbar();
	resetToolbarBehavior();
	initEntryBehavior();
	categoryFilters();
	resetCategoriesTableBehavior();
	jqueryUIButtons();
	resetSummaryBehavior("");
	resetRightBarTreeBehavior();
	awayModals();
	keyboardShortcuts();
	resetCategoriesTableBehavior();
	initAddEntryModal();
	newVersionModal();
	checkSummaryFilters();
	addRuleModal();
	addRuleFormWizard();
	instantSearch();

	$(document).ajaxStop($.unblockUI); 

	$("a#mac_notification").tipsy({gravity: 's'});
	$("button#add_entry_button").tipsy({gravity: 'n'});
	$("button#delete_summaries_button").tipsy({gravity: 'n'});
	$("button#expand_all_summaries_button").tipsy({gravity: 'n'});
	$("button#collapse_all_summaries_button").tipsy({gravity: 'n'});
	$("button#export_summaries_to_excel_button").tipsy({gravity: 'n'});
	$("button#delete_entries_button").tipsy({gravity: 'n'});
	$("button#expand_all_entries_button").tipsy({gravity: 'n'});
	$("button#collapse_all_entries_button").tipsy({gravity: 'n'});
	$("button#export_entries_to_excel_button").tipsy({gravity: 'n'});
	$("button#export_to_fb_button").tipsy({gravity: 'n'});
	$("button#export_to_bc_button").tipsy({gravity: 'n'});
	

	$("iframe").frameBorder = 0;

	$("#split_away_slider").slider({
		value: 50,
		slide: function(event, ui) {
			total_duration = 	awayEntry.finish - awayEntry.start	
			$("#seg_1_duration").html(formatSliderDuration(total_duration, ui.value/100));
			
			$("#seg_2_duration").html(formatSliderDuration(total_duration, (100-ui.value)/100));
		}

	});

	$("#annotate_away_modal").jqm({
		modal   : true,
	});

	$("#split_away_modal").jqm({
		modal   : true,
	});

	$("#duration_filter").change(function() {
		var request_data = add_category_info(this);
		$.post('/summaries/update_duration_filter', request_data, null, "script");
	});

	$("#entries_duration_filter").change(function() {
		var request_data = add_category_info(this);
		$.post('/entries/update_duration_filter', request_data, null, "script");
	});

	$("#summaries_devices_filter").change(function() {
		var request_data = add_category_info(this);
		$.post('/summaries/update_devices_filter', request_data, null, "script");
	});

	$("#entries_devices_filter").change(function() {
		var request_data = add_category_info(this);
		$.post('/entries/update_devices_filter', request_data, null, "script");
	});

	$.prettyLoader({
		animation_speed: 'fast', /* fast/normal/slow/integer */
		bind_to_ajax: true, /* true/false */
		delay: false, /* false OR time in milliseconds (ms) */
		loader: '/images/ajax-loader.gif', /* Path to your loader gif */
		offset_top: 13, /* integer */
		offset_left: 10 /* integer */
	}); 

	$('#move_to_project_button').click(function() {
		$("#projects_dropdown").css("visibility", "visible");
		if ($(this).hasClass("clicked")) {
			$(this).removeClass("clicked");
			$("#projects_dropdown").css("visibility", "hidden");
		} else {
			$(this).toggleClass("clicked");
		}
	});

	$(document).click(function(e){
		if($(e.target).is('#projects_dropdown, #projects_dropdown *, #move_to_project_button, #move_to_project_button *')) return;
		$("#projects_dropdown").css("visibility", "hidden");
		if ($('#move_to_project_button').hasClass("clicked")) {
			$('#move_to_project_button').removeClass("clicked");
		}
	});

	$('#move_to_personal_button').click(function() {
		$("#personal_dropdown").css("visibility", "visible");
		if ($(this).hasClass("clicked")) {
			$(this).removeClass("clicked");
			$("#personal_dropdown").css("visibility", "hidden");
		} else {
			$(this).toggleClass("clicked");
		}
	});

	$(document).click(function(e){
		if($(e.target).is('#personal_dropdown, #personal_dropdown *, #move_to_personal_button, #move_to_personal_button *')) return;
		$("#personal_dropdown").css("visibility", "hidden");
		if ($('#move_to_personal_button').hasClass("clicked")) {
			$('#move_to_personal_button').removeClass("clicked");
		}
	});

	$('#more_actions_button').click(function() {
		$("#more_actions_dropdown").css("visibility", "visible");
		if ($(this).hasClass("clicked")) {
			$(this).removeClass("clicked");
			$("#more_actions_dropdown").css("visibility", "hidden");
		} else {
			$(this).toggleClass("clicked");
		}
	});

	$(document).click(function(e){
		if($(e.target).is('#more_actions_dropdown, #more_actions_dropdown *, #more_actions_button, #more_actions_button *')) return;
		$("#more_actions_dropdown").css("visibility", "hidden");
		if ($('#more_actions_button').hasClass("clicked")) {
			$('#more_actions_button').removeClass("clicked");
		}
	});



	$("#edit_entry_modal").jqm({
		modal   : true
	});


		$('#annotate_away').click(function(){
	updateAwayEntryinDB();
		return false;
	});
		$('#split_away').click(function(){
	updateSplitAwayEntryinDB();
		return false;
	});



		$('#edit_manual').click(function(){
	updateManualEntryinDB();
		return false;
	});

	$("#cancel_edit_entry_button").click(function(){
		$('#edit_entry_modal').jqmHide();
		$("#edit_entry_form")[0].reset();
		$(":input").blur();
		$('#edit_entry_modal .error').hide();
	});

	$('#recent-projects li a').click(function(){
		//alert("recent project!");
		$("#projects_dropdown").css("visibility", "hidden");
		$('#move_to_project_button').removeClass("clicked");
	});

	$('#recent-categories li a').click(function(){
		//alert("recent category!");
		$("#personal_dropdown").css("visibility", "hidden");
		$('#move_to_personal_button').removeClass("clicked");
	});


	$('#add_client').submit(function() {
		$.post($(this).attr('action'),
		$(this).serializeArray(), null, 'script');
		$("#add-client-form").hide();
		$("#add-project-form").hide();
		$("#add_client")[0].reset();
		$("#add_project")[0].reset();
		$("#add-clients-projects .button").show();
		$("#add-clients-projects").show();
		$(":input").blur();
		return false;
	});

	$('#add_project').submit(function() {
		$.post($(this).attr('action'),
		$(this).serializeArray(), null, 'script');
		$("#add-client-form").hide();
		$("#add-project-form").hide();
		$("#add_client")[0].reset();
		$("#add_project")[0].reset();
		$("#add-clients-projects .button").show();
		$("#add-clients-projects").show();
		$(":input").blur();
		return false;
	});

	$('#add_personal_category').submit(function() {
		$.post($(this).attr('action'),
		$(this).serializeArray(), null, 'script');
		$("#add_personal_category")[0].reset();
		$("#add-personal-categories").hide();
		$(":input").blur();
		return false;
	});

	$("#settings-nav ul").idTabs();

	$("#categorize-tabs ul").idTabs();


	// When a node in one tree is clicked, all other nodes are unclicked	
	
	$("#toggle_search").click(function (){
		$("#search").toggle();
	});
	
	$("#toggle_sources").click(function (){
		$("#device_selection").toggle();
	});

	$("#toggle_uncategorized").click(function (){
		$("#uncategorized-tree").toggle();
	});

	$("#toggle_clients").click(function (){
		$("#clients-projects-tree").toggle();
	});

	$("#toggle_personal").click(function (){
		$("#personal-categories-tree").toggle();
	});

	// Show/hide Add Client, Add Project buttson in right nav

	$("#toggle_add_clients_projects").click(function (){
		if($("#add-clients-projects .button").css("display") == "none" && $("#add-client-form").css("display") == "none" && $("#add-project-form").css("display") == "none" ){
			$("#add-clients-projects .button").show();
		}

		$("#add-clients-projects").toggle();
		$("#add-client-form").hide();
		$("#add-project-form").hide();
	});

	$("#add_client_right_bar").click(function (){
		$("#add-clients-projects .button").hide();
		$("#add-client-form").show();
		$("#add-client-form :text:visible:enabled:first").focus();
	});

	$("#add_project_right_bar").click(function (){
		$("#add-clients-projects .button").hide();
		$("#add-project-form").show();
		$("#add-project-form :input:visible:enabled:first").focus();
	});

	$(".cancel_add_client_project").click(function (){		
		$("#add-clients-projects").toggle();
		$("#add-client-form").hide();
		$("#add-project-form").hide();
	});

	// Show/hide Add Personal Category in right nav

	$("#toggle_add_personal_categories").click(function (){
		$("#add-personal-category-form").show();
		$("#add-personal-categories").toggle();
		$("#add-personal-category-form :text:visible:enabled:first").focus();
	});

	$(".cancel_add_personal_category").click(function (){		
		$("#add-personal-categories").hide();
		$("#add-personal-category-form").hide();
	});

	$.easing.def = "easeInQuad";

	$("#export_summaries_to_excel_button").click(function(){
		var start = $(this).closest('form').find("#start").attr("value");
		var end = $(this).closest('form').find("#end").attr("value");
		window.location = 'summaries/export_to_excel?start='+start+'&end='+end;
	});

	$("#export_to_fb_button").click(function(){
		var start = $(this).closest('form').find("#start").attr("value");
		var end = $(this).closest('form').find("#end").attr("value");
		window.location = 'summaries/export_to_fb?start='+start+'&end='+end;
	});

	$("#export_to_bc_button").click(function(){
		var start = $(this).closest('form').find("#start").attr("value");
		var end = $(this).closest('form').find("#end").attr("value");
		window.location = 'basecamp_integrations/export_to_bc?start='+start+'&end='+end;
	});


	$("#export_entries_to_excel_button").click(function(){
    var start = $(this).closest('form').find("#start").attr("value");
    var end = $(this).closest('form').find("#end").attr("value");
    window.location = 'entries/export_to_excel?start='+start+'&end='+end;
	});

	$("#expand_all_summaries_button").click(function(){
		$("tr.summary-card.child").show();
		parent_node_state = new Array(0);
		$("tr.summary-card.parent").each(function(idx, item){
			parent_node_state.push($(item).attr('id'));
		});
		var temp_data = "";
		for (var i=0; i<parent_node_state.length; i++) {
			temp_data += parent_node_state[i] + ", ";
		}
		//alert(temp_data);
	});

	$("#expand_all_entries_button").click(function(){
		$("tr.summary-card.entry").show();
	});

	$("#collapse_all_summaries_button").click(function(){
		$("tr.summary-card.child").hide();
		parent_node_state = new Array(0);
	});

	$("#collapse_all_entries_button").click(function(){
		$("tr.summary-card.entry").hide();
	});




	$("#delete_summaries_button").click(function(){
		//$.blockUI();
		var active = getIds("activity[]",false);
		var aways = getIds("away_summary[]",false);
		var manuals = getIds("activity[]", true)

		if (extract_params( 'range') != "")
		$.post('/summaries/ajax_delete',{ manual:manuals, away:aways, activities: active , start:extract_params( 'start'), range:extract_params( 'range' )});
		else if (extract_params( 'end') != "")
		$.post('/summaries/ajax_delete',{  manual:manuals, away:aways, activities: active , start:extract_params( 'start'),  end:extract_params( 'end' )});
		else
		$.post('/summaries/ajax_delete',{  manual:manuals, away:aways, activities: active , start:extract_params( 'start')});
		//	request_data, null, 'script');
		//return false;
	});

	$('#undelete_summaries').click(function() {
		$.blockUI();
		$.post('/summaries/ajax_undelete',
		null, null, 'script');
		$(":input").blur();
		//return false;
	});

	$('#undelete_entries').click(function() {
		$.blockUI(); 
		$.post('/entries/ajax_undelete',
		null, null, 'script');
		$(":input").blur();
		//return false;
	});

	// Date picker

	$("#start_datepicker").datepicker({ 
		dateFormat: 'D, M d, yy',
		showAnim: ''
	});

	$("#end_datepicker").datepicker({ 
		dateFormat: 'D, M d, yy',
		showAnim: ''
	});

	$("#add_entry_datepicker").datepicker({ 
		dateFormat: 'D, M d, yy',
		showAnim: ''
	});

	$("#edit_entry_datepicker").datepicker({ 
		dateFormat: 'D, M d, yy',
		showAnim: ''
	});

	$("#range-selection").buttonset();

});

function formatSliderDuration(duration, sliderVal){
	var formattedDuration, h, m;
	formattedDuration = "";
	if (duration*sliderVal/3600 >= 1){
		h = parseInt(duration*sliderVal/3600);
		formattedDuration += h + "h ";
		m = parseInt((duration*sliderVal - 3600*h)/60);
		formattedDuration += m + "m";
	}
	else {
		m = parseInt(duration*sliderVal / 60);
		formattedDuration += m + "m";
	}
	return formattedDuration;
}

function keyboardShortcuts(){

/*	$(document).bind('keydown', {combi:'*+a', disableInInput: true}, function(){
		
		alert("dede");
	
	});
*/

	$(document).bind('keyup', {combi:'a', disableInInput: true}, function(){
		if ($('#annotate_away_modal').css('display') == 'none'){
			$('#add_entry_modal').jqmShow();
		}
	});



	$(document).bind('keyup', {combi:'c', disableInInput: true}, function(){
		$("#add-clients-projects").show();
		$("#add_client_right_bar").click();
	});

	$(document).bind('keyup', {combi:'p', disableInInput: true}, function(){
		$("#add-clients-projects").show();
		$("#add_project_right_bar").click();
	});

	$(document).bind('keyup', {combi:'del', disableInInput: true}, function(){
		$("#delete_summaries_button").click();
	});

	$(document).bind('keyup', {combi:'shift+p', disableInInput: true}, function(){
		$("#add-personal-category-form").show();
		$("#add-personal-categories").show();
		$("#add-personal-category-form :text:visible:enabled:first").focus();
	});

	$(document).bind('keydown', {combi:'Esc', disableInInput: false}, function(){
		if ($('#add_entry_modal').length != 0 && $('#add_entry_modal').css("display") != "none") {
			$("#cancel_add_entry_button").click();
		}
		if ($('#edit_entry_modal').length != 0 && $('#edit_entry_modal').css("display") != "none") {
			$("#cancel_edit_entry_button").click();
		}
		if ($('#annotate_away_modal').length != 0 && $('#annotate_away_modal').css("display") != "none") {
			$("#cancel_annotate_away_button").click();
		}
		if ($('#split_away_modal').length != 0 && $('#split_away_modal').css("display") != "none") {
			$("#cancel_split_away_button").click();
		}
		if ($('#add_client_modal').length != 0 && $('#add_client_modal').css("display") != "none") {
			$("#cancel_add_client_button").click();
		}
		if ($('#add_project_modal').length != 0 && $('#add_project_modal').css("display") != "none") {
			$("#cancel_add_project_button").click();
		}
		if ($('#add_personal_modal').length != 0 && $('#add_personal_modal').css("display") != "none") {
			$("#cancel_add_personal_button").click();
		}
		if ($('#add_rule_modal').length != 0 && $('#add_rule_modal').css("display") != "none") {
			$("#cancel_add_rule_button").click();
		}
		else {	
			if ($("#add-clients-projects .button").css("display") != "none") {
				$("#add-clients-projects").hide();
			}
			if ($("#add-client-form :input").focus() || $("#add-project-form :input").focus()) {
				$(".cancel_add_client_project").click();
				$("#add-clients-projects").hide();
			}
			if ($("#add-personal-categories :input").focus()) {
				$(".cancel_add_personal_category").click();
			}
		}
		$(":input").blur();
	});

}

function awayModals(){

	$("#annotate_away_form").submit(function(){
		//$('#selected_away_id').attr('value', selected_away_id);
		var request_data = add_category_info(this);
		$("#annotate_away_form")[0].reset();
		$(":input").blur();
		$.post('/summaries/ajax_annotate_away', request_data, null, "script");
		return false;
	});

	$("#cancel_annotate_away_button").click(function(){
		$('#annotate_away_modal').jqmHide();
		$("#annotate_away_form")[0].reset();
		$(":input").blur();
		$('#annotate_away_modal .error').hide();
		clearAwayModalData();
	});

	$("#split_away_form").submit(function(){
		$('#split_away_modal').jqmHide();
		//$('#selected_away_id').attr('value', selected_away_id);
		$('#split_away_slider_val').attr("value", $("#split_away_slider").slider("value"));
		var request_data = add_category_info(this);
		$("#split_away_form")[0].reset();
		$(":input").blur();
		$.post('/summaries/ajax_split_away', request_data, null, "script");
		$("#split_away_slider").slider("value", 50);
		return false;
	});

	$("#cancel_split_away_button").click(function(){
		$('#split_away_modal').jqmHide();
		$("#split_away_form")[0].reset();
		$(":input").blur();
		$('#split_away_modal .error').hide();
		$("#split_away_slider").slider("value", 50);
		clearAwayModalData();

	});

};

function clearAwayModalData(){
	$(".away_time").html("Loading...");
	$(".away_duration").html("");
	$("#seg_1_duration").html("");
	$("#seg_2_duration").html("");
	$(".total_time").html("Loading...");
}

function formatDateSubmit(){
	var start_date = new Date(document.selectDate.start_datepicker.value.substring(5));
	if (document.selectDate.end_datepicker == null) {
		window.location = "?start=" + start_date.format('Y-m-d');
	}
	else {
		var end_date = new Date(document.selectDate.end_datepicker.value.substring(5));
		window.location = "?start=" + start_date.format('Y-m-d') + "&end=" + end_date.format('Y-m-d');
	}
};

jQuery.fn.fadeToggle = function(speed, easing, callback) {
	return this.animate({opacity: 'toggle'}, speed, easing, callback);
};

var parent_node_state = new Array(0);
var parent_node_state_entries = new Array(0);

function resetSummaryBehavior(obj){

	$("td.name[title]").tooltip({

		// use div.tooltip as our tooltip
		//tip: '.tooltip',

		// use the fade effect instead of the default
		effect: 'fade',

		fadeInSpeed: 200,

		// make fadeOutSpeed similar to the browser's default
		fadeOutSpeed: 0,

		// the time before the tooltip is shown
		predelay: 500,

		// tweak the position
		position: "bottom right",
		offset: [-19, 116],
		tipClass: 'summary-name-tooltip'
	});

	var selected_away_id;

	$(obj+".annotate_away_button").click(function(){
		annotateAwayEntry(this)
	});

	$(obj+".split_away_button").click(function(){
		splitAwayEntry(this);
	});

	$(obj+".edit_manual_entry_button").click(function(){
		editManualEntry(this);
		

	});

	// Show/hide children summary-card nodes

	$(obj+".parent td.name")
	.css("cursor", "pointer")
	.click(function (){
		$(this).parent().siblings('.'+this.parentNode.id).toggle();
		if ($(this).parent().siblings('.'+this.parentNode.id).css("display") != "none") {
			parent_node_state.push(this.parentNode.id);
		}
		else {
			parent_node_state.splice(parent_node_state.indexOf(this.parentNode.id), 1);
		}
		var temp_data = "";
		for (var i=0; i<parent_node_state.length; i++) {
			temp_data += parent_node_state[i] + ", ";
		}
	});

	$(obj+".summary-card.child").css("display", function(){
		if (parent_node_state.indexOf($(this).attr("class").substring(19)) != -1)
		return "table-row";
		else
		return "none";
	});

	$(obj+".parent td.name")
	.mouseover(function (){
		$(this).find("div").show();
	});

	$(obj+".parent td.name")
	.mouseout(function (){
		$(this).find("div").hide();
	});

	$(obj+"td.away")
	.hover(function (){
		$(this).find(".away-buttons").toggle();
	});

	$(obj+"td.active")
	.hover(function (){
		$(this).find(".active-buttons").toggle();
	});

	$(obj+"td.away input")
	.focus(function (){
		$(this).find("div").hide();
	});

	$(obj+"td.away input").focus(function (){
		$(this).find("div").hide();
	});

	//$(obj+"td.name .note").hide();

	$(obj+"td.name .away-buttons").hide();

	$(obj+"td.name .active-buttons").hide();


	// Checkbox and row highlighting

	$(obj+'tr.summary-card')
	.filter(':has(:checkbox:checked)')
	.addClass('selected')
	.end()
	.click(function() {
	});

	$(obj+'.checkbox input:checkbox')
	.click(function(event) {
		$(this).closest("tr").toggleClass('selected');
		if ($(this).attr('checked')){
			$(this).parent().siblings(".name").children().closest(".active-buttons, .away-buttons").css("background-color", "#ffc");
		} else {
			if ($(this).parent().parent().hasClass("activity")){
				$(this).parent().siblings(".name").children().closest(".active-buttons, .away-buttons").css("background-color", "#fff");
			} else {	
				$(this).parent().siblings(".name").children().closest(".active-buttons, .away-buttons").css("background-color", "#f5f5f5");
			}
		}

	});


	// When a parent node checkbox is checked, the corresponding children nodes' checkboxes are checked as well. Uncheck the parent node's checkbox, and all children nodes' 
	// checkboxes become unchecked

	$(obj+'.parent .checkbox input:checkbox')
	.click(function() {
		if ($(this).attr('checked')){
			$(this).parent().siblings(".name").children().closest(".note").css("background-color", "#ffc");
			$(this).parent().parent().siblings('.'+this.parentNode.parentNode.id).children().closest(".checkbox").children().closest("input:checkbox").attr('checked', true);
			$(this).parent().parent().siblings('.'+this.parentNode.parentNode.id).children().each(function(idx, item) {
				if (!($(item).closest(".checkbox").closest("tr").hasClass('selected'))){
					$(item).closest(".checkbox").closest("tr").toggleClass('selected');
					$(item).closest(".checkbox").closest("tr").children().closest(".name").children().closest(".active-buttons, .away-buttons").css("background-color", "#ffc");
				}
			});
		}
		else {
			$(this).parent().siblings(".name").children().closest(".note").css("background-color", "#fff");
			$(this).parent().parent().siblings('.'+this.parentNode.parentNode.id).children().closest(".checkbox").children().closest("input:checkbox").attr('checked', false);
			$(this).parent().parent().siblings('.'+this.parentNode.parentNode.id).children().each(function(idx, item) {
				if (($(item).closest(".checkbox").closest("tr").hasClass('selected'))){
					$(item).closest(".checkbox").closest("tr").toggleClass('selected');
					$(item).closest(".checkbox").closest("tr").children().closest(".name").children().closest(".active-buttons, .away-buttons").css("background-color", "#f5f5f5");
				}
			});
		}
	});

	// When a child node checkbox is unchecked, the parent node's checkbox is automatically unchecked

	$(obj+'.child .checkbox input:checkbox')
	.click(function() {
		if (!($(this).attr('checked'))){
			$(this).parent().parent().siblings(".parent").children().closest(".checkbox").children().closest("input:checkbox").attr('checked', false);
			$(this).parent().parent().siblings(".parent").children().closest(".name").children(".note").css("background-color", "#fff");
			$(this).parent().parent().siblings(".parent").each(function(idx, item) {
				if (($(item).hasClass('selected'))){
					$(item).toggleClass('selected');
				}	
			});
		}

	});

};

function initEntryBehavior(){

	$("td.short-name[title]").tooltip({

		// use div.tooltip as our tooltip
		//tip: '.tooltip',

		// use the fade effect instead of the default
		effect: 'fade',

		fadeInSpeed: 200,

		// make fadeOutSpeed similar to the browser's default
		fadeOutSpeed: 0,

		// the time before the tooltip is shown
		predelay: 500,

		// tweak the position
		position: "bottom right",
		offset: [-12, 40],
		tipClass: 'summary-name-tooltip'
	});

	$(".summary-card-title.entries").each(function(idx, item){
		if ($(item).siblings().size() == 0) {
			$(item).remove();
		}
		$("#entries_data").show();
	});

	$(".summary-card-title.entries")
	.css("cursor", "pointer")
	.click(function (){
		$(this).siblings('.summary-card.entry').toggle();
	});

	$(".timestamps-category span[title]").tooltip({
		position: 'top right',
		offset: [0, 0],
		delay: 0,
		tipClass: 'timestamps-category-tooltip'
	});

	var lastChecked = null;

	$(".entry_checkbox").click(function(event) {
		if (!lastChecked) {
			lastChecked = this;
			return;
		}

		if (event.shiftKey) {
			var start = $('.entry_checkbox').index(this);
			var end = $('.entry_checkbox').index(lastChecked);

			$('.entry_checkbox').slice(Math.min(start,end), Math.max(start,end)+1).attr('checked', lastChecked.checked);

			//$('.entry_checkbox').slice(Math.min(start,end)+1, Math.max(start,end)).parent().parent().toggleClass('selected');
			$('.entry_checkbox').slice(Math.min(start,end)+1, Math.max(start,end)).each(function(idx, item){
				if ($(item).is(':checked') && $(item).parent().parent().hasClass('selected') == false){
					$(item).parent().parent().addClass('selected');
				}
				else if ($(item).is(':checked') == false && $(item).parent().parent().hasClass('selected')) {
					$(item).parent().parent().removeClass('selected');
				}
			});
		}

		lastChecked = this;
	});
}

function resetEntryBehavior(){

	initEntryBehavior();

	$('tr.summary-card.entry')
	.filter(':has(:checkbox:checked)')
	.addClass('selected')
	.end()
	.click(function() {
	});

	$('.checkbox input:checkbox')
	.click(function(event) {
		$(this).closest("tr").toggleClass('selected');
		if (event.target.type !== 'checkbox') {
			$(':checkbox', this).attr('checked', function() {
				return !this.checked;
			});
		}
	});
}

function showClickedProject(){
	$('li.project a.clicked').closest('ul').css('display', 'block');
}

function resetRightBarTreeBehavior(){
	// Expand/collapse children in right bar

	showClickedProject();

	$(".panel li .triangle")
	.css("cursor", "pointer")
	.click(function (){
		$(this).parent().children("ul").toggle();
		if ($(this).hasClass("closed")) {
			$(this).removeClass('closed').addClass('open');
		} else if ($(this).hasClass("open")) {
			$(this).removeClass('open').addClass('closed');
		}
	});

	$(".panel li a")
	.css("cursor", "pointer")
	.dblclick(function (){
		$(this).parent().children("ul").toggle();
		if ($(this).siblings('.triangle').hasClass("closed")) {
			$(this).siblings('.triangle').removeClass('closed').addClass('open');
		} else if ($(this).siblings('.triangle').hasClass("open")) {
			$(this).siblings('.triangle').removeClass('open').addClass('closed');
		}
	});

	$(".panel li a")
	.css("cursor", "pointer")
	.click(function (){
		if ($(this).hasClass("clicked")) {
		} else {
			$(".panel li a.clicked").removeClass('clicked');
			$(".panel li .triangle.clicked").removeClass('clicked');
			$(this).addClass('clicked');
			$(this).parent().children(".open").addClass('clicked');
			$(this).parent().children(".closed").addClass('clicked');
			$(this).parent().siblings().children('.open').removeClass('clicked');
			$(this).parent().siblings().children('.closed').removeClass('clicked');
			if ($(this).parent().hasClass('project')){
				$(this).parent().parent().parent().children('.open').removeClass('clicked');
				$(this).parent().parent().parent().siblings().children('.open').removeClass('clicked');
				$(this).parent().parent().parent().children('.closed').removeClass('clicked');
				$(this).parent().parent().parent().siblings().children('.closed').removeClass('clicked');
			}
		}
	});

};






function resetToolbarBehavior(){

	$('#projects-tree-summaries ul li a').click(function(){
		$.blockUI(); 
		$('#category_type').attr('value', '1');
		$('#button_clicked').attr('value', 'categorize');
		$('#category_id').attr('value', $(this).attr('id'));
	ajax_categorize_summaries($(this).attr('id'),$(this).text());  
		$("#projects_dropdown").css("visibility", "hidden");
		$('#move_to_project_button').removeClass("clicked");
		//return false;
	});

	$('#projects-tree-entries ul li a').click(function(){
		$.blockUI(); 
		$('#category_type').attr('value', '1');
		$('#button_clicked').attr('value', 'categorize_to_project');
		$('#category_id').attr('value', $(this).attr('id'));
		
		ajax_categorize_summaries($(this).attr('id'),$(this).text());  
		
		//var request_data = add_category_info(this);
		//$.post('/entries/ajax_categorize', request_data, null, "script");  
		$("#projects_dropdown").css("visibility", "hidden");
		$('#move_to_project_button').removeClass("clicked");
		//return false;
	});

	$('#personal-tree-summaries li a').click(function(){
		$.blockUI(); 
		$('#category_type').attr('value', '2');
		$('#button_clicked').attr('value', 'categorize');
		$('#category_id').attr('value', $(this).attr('id'));
		ajax_categorize_summaries($(this).attr('id'),$(this).text());
		$("#personal_dropdown").css("visibility", "hidden");
		$('#move_to_personal_button').removeClass("clicked");
		//return false;
	});

	$('#personal-tree-entries li a').click(function(){
		$.blockUI(); 
		$('#category_type').attr('value', '2');
		$('#button_clicked').attr('value', 'categorize_to_personal');
		$('#category_id').attr('value', $(this).attr('id'));
	
		ajax_categorize_summaries($(this).attr('id'),$(this).text());  
	
	//	var request_data = add_category_info(this);
//		$.post('/entries/ajax_categorize', request_data, null, "script");
		$("#personal_dropdown").css("visibility", "hidden");
		$('#move_to_personal_button').removeClass("clicked");
		//return false;
	});

	$('#recent-projects-summaries li a').click(function(){
		$.blockUI(); 
		$('#category_type').attr('value', '1');
		$('#button_clicked').attr('value', 'categorize');
		$('#category_id').attr('value', $(this).attr('id'));
	ajax_categorize_summaries($(this).attr('id'),$(this).text());
		$("#projects_dropdown").css("visibility", "hidden");
		$('#move_to_project_button').removeClass("clicked");
		//return false;
	});

	$('#recent-projects-entries li a').click(function(){
		$.blockUI(); 
		$('#category_type').attr('value', '1');
		$('#button_clicked').attr('value', 'categorize_to_project');
		$('#category_id').attr('value', $(this).attr('id'));
		ajax_categorize_summaries($(this).attr('id'),$(this).text());  
	
	
	//	var request_data = add_category_info(this);
//		$.post('/entries/ajax_categorize', request_data, null, "script");
		$("#projects_dropdown").css("visibility", "hidden");
		$('#move_to_project_button').removeClass("clicked");
		//return false;
	});

	$('#recent-categories-summaries li a').click(function(){
		$.blockUI(); 
		$('#category_type').attr('value', '2');
		$('#button_clicked').attr('value', 'categorize');
		$('#category_id').attr('value', $(this).attr('id'));
	ajax_categorize_summaries($(this).attr('id'),$(this).text());
		$("#personal_dropdown").css("visibility", "hidden");
		$('#move_to_personal_button').removeClass("clicked");
		//return false;
	});

	$('#recent-categories-entries li a').click(function(){
		$.blockUI(); 
		$('#category_type').attr('value', '2');
		$('#button_clicked').attr('value', 'categorize_to_personal');
		$('#category_id').attr('value', $(this).attr('id'));
		ajax_categorize_summaries($(this).attr('id'),$(this).text());  
	
	
		//var request_data = add_category_info(this);
		//$.post('/entries/ajax_categorize', request_data, null, "script");
		$("#personal_dropdown").css("visibility", "hidden");
		$('#move_to_personal_button').removeClass("clicked");
		//return false;
	});

	$('#more_actions ul li a#uncategorize-summaries').click(function(){
		$.blockUI(); 
		$('#category_type').attr('value', null);
		$('#button_clicked').attr('value', 'categorize');
		$('#category_id').attr('value', null);
		var request_data = add_category_info(this);
		ajax_categorize_summaries(0,$(this).text());
		
		$.post('/summaries/ajax_categorize', request_data, null, "script");  
		$("#more_actions_dropdown").css("visibility", "hidden");
		$('#more_actions_button').removeClass("clicked");
		//return false;
	});

	$('#more_actions ul li a#uncategorize-entries').click(function(){
		$.blockUI(); 
		$('#category_type').attr('value', null);
		$('#button_clicked').attr('value', 'categorize_to_project');
		$('#category_id').attr('value', null);
		ajax_categorize_summaries($(this).attr('id'),$(this).text());  
		
		$("#more_actions_dropdown").css("visibility", "hidden");
		$('#more_actions_button').removeClass("clicked");
		//return false;
	});
}

function checkDescription(){
	if ($(this).html().substr(-1) == ")") {
		$(this).siblings("td.icon").children("img").attr("src", "/images/famfamfam/accept.png");
	}
	else {
		$(this).siblings("td.icon").children("img").attr("src", "/images/app_icons/Icons_small/Screen Off.png");
	}
};

function get_selected_category(){
	var cat;
	$(".clicked").each(function(){
		cat = $(this).parent().get(0).id;
	})
	return cat;		
}

function get_category_type(){
	var type;
	$(".selected").each(function(){
		var href = $(this).attr("href")
		if (href != undefined) {
			if (href == "#personal_tab") type =2;
			else type =1;	
		}
	})

	return type;
}

function add_category_info(obj){
	//var dataArray = new Object();
	var form = $(obj).closest('form').serialize();
	return form;
}

function enable_undelete_summaries(){	
	$('#undelete_summaries').click(function() {
		var request_data = add_category_info("#delete_summaries_button");
		$.post('/summaries/ajax_undelete',
		request_data, null, 'script');
		$(":input").blur();
		$(".top-flash").html("");
		//return false;
	});
}

function enable_undelete_entries(){	
	$('#undelete_entries').click(function() {
		var request_data = add_category_info("#delete_entries_button");
		$.post('/entries/ajax_undelete',
		request_data, null, 'script');
		$(":input").blur();
		$(".top-flash").html("");
		//return false;
	});
}

function clear_highlighted_categories(){
	$(".panel li a.clicked").removeClass("clicked");
};

function categoryFilters(){

	// Summaries Views

	$("#show_all_summaries_button a").click(function(){
		$("#cat_filter").attr("value", "false");
		var request_data = add_category_info(this);
		$.post('/summaries/refresh_summary', request_data, null, "script");
		$(".panel li a.clicked").removeClass("clicked");
		$(".panel li .triangle.clicked").removeClass('clicked');
		$("div#uncategorized-data").show();
		$("div#client-summaries").show();
		$("div#personal-summaries").show();
		$("div#away-uncat").show();
		$("div#active-uncat").show();
		$("tr.project-row").show();
		$("div.client-table").show();
	});

	$("a.client_node_summaries").click(function(){
		client_id = this.id
		$("#client_filter").attr("value", client_id);
		var request_data = add_category_info(this);
		$.post('/summaries/refresh_summary', request_data, null, "script");
		$("tr.project-row").show();
		$("div#uncategorized-data").hide();
		$("div#personal-summaries").hide();
		$("div#client-summaries").show();
		if (client_id != "all"){
			$("div.client-table").hide();
			$("div#client_"+client_id).show();
		} else {
			$("div.client-table").show();
		}
	});

	$("a.project_node_summaries").click(function(){
		project_id = this.id;
		$("#project_filter").attr("value", project_id);
		var request_data = add_category_info(this);
		$.post('/summaries/refresh_summary', request_data, null, "script");
		$("div#uncategorized-data").hide();
		$("div#personal-summaries").hide();
		$("div#client-summaries").show();
		related_client_id = $(this).parent().parent().siblings("a").attr('id');
		$("div.client-table").hide();
		$("div#client_"+related_client_id).show();
		$("tr.project-row").hide();
		$("tr.project-contents").hide();
		$("tr#project_"+project_id).show();
	});

	$("a.personal_node_summaries").click(function(){
		personal_id = this.id
		$("#personal_filter").attr("value", personal_id);
		var request_data = add_category_info(this);
		$.post('/summaries/refresh_summary', request_data, null, "script");
		$("div#uncategorized-data").hide();
		$("div#client-summaries").hide();
		$("div#personal-summaries").show();
		if (personal_id != "all"){
			$("div.personal-table").hide();
			$("div#personal_"+personal_id).show();
		} else {
			$("div.personal-table").show();
		}
	});

	$("a#uncat_active_node_summaries").click(function(){
		$("#uncategorized_filter").attr("value", "active");
		var request_data = add_category_info(this);
		$.post('/summaries/refresh_summary', request_data, null, "script");
		$("div#uncategorized-data").show();
		$("div#client-summaries").hide();
		$("div#personal-summaries").hide();
		$("div#away-uncat").hide();
		$("div#active-uncat").show();
	});

	$("a#uncat_away_node_summaries").click(function(){
		$("#uncategorized_filter").attr("value", "away");
		var request_data = add_category_info(this);
		$.post('/summaries/refresh_summary', request_data, null, "script");
		$("div#uncategorized-data").show();
		$("div#client-summaries").hide();
		$("div#personal-summaries").hide();
		$("div#active-uncat").hide();
		$("div#away-uncat").show();
	});

	$("a#uncat_all_node_summaries").click(function(){
		$("#uncategorized_filter").attr("value", "all");
		var request_data = add_category_info(this);
		$.post('/summaries/refresh_summary', request_data, null, "script");
		$("div#uncategorized-data").show();
		$("div#client-summaries").hide();
		$("div#personal-summaries").hide();
		$("div#active-uncat").show();
		$("div#away-uncat").show();
	});

	// Entries Views

	$("#show_all_entries_button a").click(function(){
		$("#cat_filter").attr("value", "false");
		var request_data = add_category_info(this);
		$.post('/entries/refresh_entries', request_data, null, "script");
		$(".panel li a.clicked").removeClass("clicked");
		$(".panel li .triangle.clicked").removeClass('clicked');
	});

	$("a.client_node_entries").click(function(){
		client_id = this.id
		$("#client_filter").attr("value", client_id);
		var request_data = add_category_info(this);
		$.post('/entries/refresh_entries', request_data, null, "script");
	});

	$("a.project_node_entries").click(function(){
		project_id = this.id
		$("#project_filter").attr("value", project_id);
		var request_data = add_category_info(this);
		$.post('/entries/refresh_entries', request_data, null, "script");
	});

	$("a.personal_node_entries").click(function(){
		personal_id = this.id
		$("#personal_filter").attr("value", personal_id);
		var request_data = add_category_info(this);
		$.post('/entries/refresh_entries', request_data, null, "script");
	});

	$("a#uncat_active_node_entries").click(function(){
		$("#uncategorized_filter").attr("value", "active");
		var request_data = add_category_info(this);
		$.post('/entries/refresh_entries', request_data, null, "script");
	});

	$("a#uncat_away_node_entries").click(function(){
		$("#uncategorized_filter").attr("value", "away");
		var request_data = add_category_info(this);
		$.post('/entries/refresh_entries', request_data, null, "script");
	});

	$("a#uncat_all_node_entries").click(function(){
		$("#uncategorized_filter").attr("value", "all");
		var request_data = add_category_info(this);
		$.post('/entries/refresh_entries', request_data, null, "script");
	});

}

function addClientModal(){
	$("#add_client_modal").jqm({
		modal   : true,
		trigger : '#add_client_button'
	});

	$("#add_client_button").click(function(){
		$("#add_client_modal form :text:visible:enabled:first").focus();
	});

	$('#add_client_form').submit(function(){
		var request_data = add_category_info(this);
		$("#add_client_form")[0].reset();
		$(":input").blur();
		$.post('/clients/ajax_add_client', request_data, null, "script");
		return false;
	});

	$("#cancel_add_client_button").click(function(){
		$('#add_client_modal').jqmHide();
		$("#add_client_form")[0].reset();
		$(":input").blur();
		$('#add_client_modal .error').hide();
	});
}

function addProjectModal(){
	$("#add_project_modal").jqm({
		modal   : true,
		trigger : '.add_project_button'
	});

	$(".add_project_button").click(function(){
		$("#add_project_modal").jqmShow();
		$("#add_project_modal form :text:visible:enabled:first").focus();
		$("#client").attr("value", this.id);
	});

	$('#add_project_form').submit(function(){
		var request_data = add_category_info(this);
		$("#add_project_form")[0].reset();
		$(":input").blur();
		$.post('/projects/ajax_add_project', request_data, null, "script");
		return false;
	});

	$("#cancel_add_project_button").click(function(){
		$('#add_project_modal').jqmHide();
		$("#add_project_modal")[0].reset();
		$(":input").blur();
		$('#add_project_modal .error').hide();
	});
}

function addPersonalModal(){
	$("#add_personal_modal").jqm({
		modal   : true,
		trigger : '#add_personal_button'
	});

	$("#add_personal_button").click(function(){
		$("#add_personal_modal form :text:visible:enabled:first").focus();
	});

	$('#add_personal_form').submit(function(){
		var request_data = add_category_info(this);
		$("#add_personal_form")[0].reset();
		$(":input").blur();
		$.post('/personal_categories/ajax_add_personal', request_data, null, "script");
		return false;
	});

	$("#cancel_add_personal_button").click(function(){
		$('#add_personal_modal').jqmHide();
		$("#add_personal_modal")[0].reset();
		$(":input").blur();
		$('#add_personal_modal .error').hide();
	});
}

function addRuleModal(){
	$("#add_rule_modal").jqm({
		modal   : true,
		trigger : '#add_rule_button'
	});
	
	$("#add_rule_button").click(function(){
		if ($("#add_rule_form").formwizard("state").currentStep != "first"){
			$("#add_rule_form").formwizard("show","first");
		};
		$("button#next").html("Next Step &raquo;");
		$("#add_rule_modal form :text:visible:enabled:first").focus();
		$("#rule_start_datepicker").datepicker({ 
			dateFormat: 'D, M d, yy',
			showAnim: ''
		});

		$("#rule_end_datepicker").datepicker({ 
			dateFormat: 'D, M d, yy',
			showAnim: ''
		});
		
		$("#rule_before_datepicker").datepicker({ 
			dateFormat: 'D, M d, yy',
			showAnim: ''
		});
		
		$("#rule_after_datepicker").datepicker({ 
			dateFormat: 'D, M d, yy',
			showAnim: ''
		});
		
		$("#rule_start_datepicker").datepicker("hide");
		$("#rule_end_datepicker").datepicker("hide");
		$("#rule_before_datepicker").datepicker("hide");
		$("#rule_after_datepicker").datepicker("hide");
	});
	
	$("#cancel_add_rule_button").click(function(){
		$('#add_rule_modal').jqmHide();
		$(":input").blur();
		$('#add_rule_form').resetForm();
		$("#rule_start_datepicker").datepicker("hide");
		$("#rule_end_datepicker").datepicker("hide");
		$("#rule_before_datepicker").datepicker("hide");
		$("#rule_after_datepicker").datepicker("hide");
		
	});
	
	$("#add_rule_form").bind("step_shown", function(event, data){
			if (data.isLastStep == true) {
				$("button#next").html("Create This Rule &raquo;");
				$("#rule_start_datepicker").datepicker("hide");
				$("#rule_end_datepicker").datepicker("hide");
			}
			else {
				$("button#next").html("Next Step &raquo;");
			}
			$("#add_rule_modal form :visible:enabled:first").focus();
	});

}

function addRuleFormWizard(){
	$("#add_rule_form").formwizard({ 
	 	formPluginEnabled: true,
	 	validationEnabled: true,
	 	focusFirstInput : true,
		disableUIStyles : true,
		inDuration: 0,
		outDuration: 0,
		textSubmit: 'Finish',
		formOptions: {
			reset: true,
			url: 'rules/create',
			type: 'POST',
			success: function(data){
				$('#add_rule_modal').jqmHide();
				$(":input").blur();
				$('#add_rule_form').resetForm();
				$("#rule_start_datepicker").datepicker("hide");
				$("#rule_end_datepicker").datepicker("hide");
				$("#rule_before_datepicker").datepicker("hide");
				$("#rule_after_datepicker").datepicker("hide");
				window.location.reload();
			}
		}
	 }
	);
}

function flash_controls(){
	$(".close_flash").click(function(){
		$(".top-flash").hide();	
	});
}

function jqueryUIButtons(){
	// buttons

	$("a.edit").button({
		icons: {
			primary: 'ui-icon-pencil'
		},

		text: false
	});

	$("a.show").button({
		icons: {
			primary: 'ui-icon-info'
		},

		text: false		
	});

	
	$("a.delete").button({
		icons: {
			primary: 'ui-icon-trash'
		},

		text: false

	});
}

function reset_category_filters(){
	$("#client_filter").attr("value", "");
	$("#project_filter").attr("value", "");
	$("#personal_filter").attr("value", "");
	$("#uncategorized_filter").attr("value", "");
}

function floatingToolbar() {


	var placeholder = $("#toolbar-placeholder");
	var toolbar = $("#toolbar");
	var view = $(document);

	view.bind(
		"scroll resize",
		function(){

			// Get the current offset of the placeholder.
			// Since the message might be in fixed
			// position, it is the plcaeholder that will
			// give us reliable offsets.
			if (placeholder.offset() != null) {
				var placeholderTop = placeholder.offset().top;
			}

			// Get the current scroll of the window.
			var viewTop = view.scrollTop();

			// Check to see if the view had scroll down
			// past the top of the placeholder AND that
			// the message is not yet fixed.
			if (
				(viewTop > placeholderTop) &&
				!toolbar.is( ".toolbar-fixed" )
			){

				// The message needs to be fixed. Before
				// we change its positon, we need to re-
				// adjust the placeholder height to keep
				// the same space as the message.
				//
				// NOTE: All we're doing here is going
				// from auto height to explicit height.
				placeholder.height(
					placeholder.height()
				);

				// Make the message fixed.
				toolbar.addClass( "toolbar-fixed" );

				// Check to see if the view has scroll back up
				// above the message AND that the message is
				// currently fixed.
				} else if (
					(viewTop <= placeholderTop) &&
					toolbar.is( ".toolbar-fixed" )
				){

					// Make the placeholder height auto again.
					placeholder.css( "height", "52px" );

					// Remove the fixed position class on the
					// message. This will pop it back into its
					// static position.
					toolbar.removeClass( "toolbar-fixed" );

				}
			}
		);
	};

	function splitString(myString){
		mySplitResult = myString.split(" ");
		var result = "";
		if (mySplitResult.length > 1) {
			for (i = 0; i < mySplitResult.length - 1; i++){
				result = result + mySplitResult[i].replace(/^\s*|\s*$/g,'') + "<br/>";
			}
			result = result + mySplitResult[i].replace(/^\s*|\s*$/g,'')
		}
		else {
			result = mySplitResult;
		}
		return result;
	};

	function formatTime(d){
		var a_p = "";

		var curr_hour = d.getUTCHours();

		if (curr_hour < 12)
		{
			a_p = "am";
		}
		else
		{
			a_p = "pm";
		}
		if (curr_hour == 0)
		{
			curr_hour = 12;
		}
		if (curr_hour > 12)
		{
			curr_hour = curr_hour - 12;
		}

		var curr_min = d.getMinutes();

		curr_min = curr_min + "";

		if (curr_min.length == 1)
		{
			curr_min = "0" + curr_min;
		}


		return(curr_hour + ":" + curr_min + " " + a_p);


	};

	function resetCategoriesTableBehavior(){
		addClientModal();
		addProjectModal();
		addPersonalModal();
	};

	function displayDurationHM(totalSeconds){

		var days = parseInt(totalSeconds / 86400);
		var hours = parseInt((totalSeconds / 3600) - (days * 24));
		var minutes = parseInt((totalSeconds / 60) - (hours * 60) - (days * 1440));

		var display = '';
		var display_concat = '';
		if (days > 0){
			display = display + display_concat + days + "d";
			display_concat = ' ';
		}
		if (hours > 0 || display.length > 0){
			display = display + display_concat + hours + "h";
			display_concat = ' ';
		}
		display = display + display_concat + minutes + "m";
		return display;
	};

	function initAddEntryModal(){
		$("#add_entry_modal").jqm({
			modal   : true,
			trigger : '#add_entry_button'
		});


		$("#add_entry_button").click(function(){
			$("#add_entry_modal form :text:visible:enabled:first").focus();
		});

		$('#add_entry_form').submit(function(){ 
			var request_data = add_category_info(this);
			$("#add_entry_form")[0].reset();
			$(":input").blur();
			$.post('/summaries/ajax_add_entry', request_data, null, "script");
			return false;
		});

		$("#cancel_add_entry_button").click(function(){
			$('#add_entry_modal').jqmHide();
			$("#add_entry_form")[0].reset();
			$(":input").blur();
			$('#add_entry_modal .error').hide();
		});
	}

	function newVersionModal() {
		$("#new_version_modal").jqm({
			modal   : true,
		});
		$("#new_version_modal").css("display", "block");
	}

	function instantSearch() {
		$("input").keyup(function () {
		if ($('input#search_box').val() != "") {
			$('input#search_box').quicksearch('table.summary-container tr.summary-card', {
				'bind': 'keyup click',
				'onAfter': function(){
					if ($('input#search_box').val() == "") {
						$("tr.summary-card.child").hide();
						$("tr.summary-card.entry").show();
					}
				}
			});
		}
		});
		
		
	}

	function disableEnterKey(e)
	{
	     var key;
	     if(window.event)
	          key = window.event.keyCode;     //IE
	     else
	          key = e.which;     //firefox
	     if(key == 13)
	          return false;
	     else
	          return true;
	}

	function displayDuration(seconds) {
		var h = Math.floor(seconds / 3600);
		var m = Math.floor((seconds - (h * 3600))/60)
		var s = seconds - (h * 3600) - (m * 60);
		var output = "";
		if (h > 0){
			output += h + "h ";
		}
		output += m + "m ";
		output += s + "s";
		return output;
	}

	function checkSummaryFilters() {
		if ($("a.client_node_summaries.clicked").size() == 1 ) {
			client_id = $("a.client_node_summaries.clicked").attr("id");
			//$("#client_filter").attr("value", client_id);
			$("tr.project-row").show();
			$("div#uncategorized-data").hide();
			$("div#personal-summaries").hide();
			$("div#client-summaries").show();
			if (client_id != "all"){
				$("div.client-table").hide();
				$("div#client_"+client_id).show();
			} else {
				$("div.client-table").show();
			}
		}
		else if ($("a.project_node_summaries.clicked").size() == 1 ) {
			project_id = $("a.project_node_summaries.clicked").attr("id");
			//$("#project_filter").attr("value", project_id);
			$("div#uncategorized-data").hide();
			$("div#personal-summaries").hide();
			$("div#client-summaries").show();
			related_client_id = $("a.project_node_summaries.clicked").parent().parent().siblings("a").attr('id');
			$("div.client-table").hide();
			$("div#client_"+related_client_id).show();
			$("tr.project-row").hide();
			$("tr.project-contents").hide();
			$("tr#project_"+project_id).show();
		}
		else if ($("a.personal_node_summaries.clicked").size() == 1 ) {
			personal_id = $("a.personal_node_summaries.clicked").attr("id");
			//$("#personal_filter").attr("value", personal_id);
			$("div#uncategorized-data").hide();
			$("div#client-summaries").hide();
			$("div#personal-summaries").show();
			if (personal_id != "all"){
				$("div.personal-table").hide();
				$("div#personal_"+personal_id).show();
			} else {
				$("div.personal-table").show();
			}
		}
		else if ($("a#uncat_all_node_summaries.clicked").size() == 1 ) {
			//$("#uncategorized_filter").attr("value", "all");
			$("div#uncategorized-data").show();
			$("div#client-summaries").hide();
			$("div#personal-summaries").hide();
			$("div#active-uncat").show();
			$("div#away-uncat").show();
		}
		else if ($("a#uncat_active_node_summaries.clicked").size() == 1 ) {
			//$("#uncategorized_filter").attr("value", "active");
			$("div#uncategorized-data").show();
			$("div#client-summaries").hide();
			$("div#personal-summaries").hide();
			$("div#away-uncat").hide();
			$("div#active-uncat").show();
		}
		else if ($("a#uncat_away_node_summaries.clicked").size() == 1 ) {
			//$("#uncategorized_filter").attr("value", "away");
			$("div#uncategorized-data").show();
			$("div#client-summaries").hide();
			$("div#personal-summaries").hide();
			$("div#active-uncat").hide();
			$("div#away-uncat").show();
		}
	}
